using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace sforceExplorer
{
	public class frmResetPassword : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ListBox listBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button btnReset;
		private System.Windows.Forms.Button btnClose;
		private System.ComponentModel.Container components = null;
		private string _setOrReset = "reset";

		public frmResetPassword(string setOrReset)
		{
			InitializeComponent();
			_setOrReset = setOrReset;
			if (_setOrReset.Equals("reset")) 
				this.btnReset.Text = "Reset";
			else
				this.btnReset.Text = "Set New";
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.label1 = new System.Windows.Forms.Label();
			this.btnReset = new System.Windows.Forms.Button();
			this.btnClose = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// listBox1
			// 
			this.listBox1.Location = new System.Drawing.Point(8, 64);
			this.listBox1.Name = "listBox1";
			this.listBox1.Size = new System.Drawing.Size(280, 251);
			this.listBox1.TabIndex = 0;
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(200, 32);
			this.label1.TabIndex = 1;
			this.label1.Text = "Select the user below whose password you want to reset, then click reset.";
			// 
			// btnReset
			// 
			this.btnReset.Location = new System.Drawing.Point(72, 328);
			this.btnReset.Name = "btnReset";
			this.btnReset.Size = new System.Drawing.Size(72, 24);
			this.btnReset.TabIndex = 2;
			this.btnReset.Text = "Reset";
			this.btnReset.Click += new System.EventHandler(this.btnReset_Click);
			// 
			// btnClose
			// 
			this.btnClose.Location = new System.Drawing.Point(160, 328);
			this.btnClose.Name = "btnClose";
			this.btnClose.Size = new System.Drawing.Size(72, 24);
			this.btnClose.TabIndex = 2;
			this.btnClose.Text = "Close";
			this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
			// 
			// frmResetPassword
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(296, 358);
			this.ControlBox = false;
			this.Controls.Add(this.btnReset);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.listBox1);
			this.Controls.Add(this.btnClose);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Name = "frmResetPassword";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "Reset Password Sample";
			this.Load += new System.EventHandler(this.frmResetPassword_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void frmResetPassword_Load(object sender, System.EventArgs e)
		{
			sforce.QueryResult qr = Utility.binding.query("Select Id, FirstName, LastName, Username from User");

			if (qr.size > 0) 
			{
				bool continueLoop = true;
				while (continueLoop) 
				{
					for (int i=0;i<qr.records.Length;i++)
						this.listBox1.Items.Add(new lbItem(qr.records[i], new string[] {"FirstName", "LastName", "Username"}));
					if (!qr.done) 
						qr = Utility.binding.queryMore(qr.queryLocator);
					else
						continueLoop = false;
				}
			}
		}

		private void btnClose_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void btnReset_Click(object sender, System.EventArgs e)
		{
			if (_setOrReset.Equals("reset"))
			{
				if (System.Windows.Forms.MessageBox.Show("Please confirm that you want to reset the password for " + this.listBox1.Text + ".", "Partner Sample", System.Windows.Forms.MessageBoxButtons.OKCancel, System.Windows.Forms.MessageBoxIcon.Question).Equals(System.Windows.Forms.DialogResult.OK)) 
				{
					try 
					{
						sforce.ResetPasswordResult rpr = Utility.binding.resetPassword(((lbItem)this.listBox1.SelectedItem).item.Id);
						System.Windows.Forms.MessageBox.Show("Reset password was successful!" + Environment.NewLine + Environment.NewLine + "The new password is " + rpr.password + ".", "Partner Sample", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
					} 
					catch (Exception ex) 
					{
						System.Windows.Forms.MessageBox.Show("Password was not reset: " + ex.Message, "Partner Sample", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
					}
				}
			}
			else 
			{
				frmPasswordEntry frmPEnt = new frmPasswordEntry(((lbItem)this.listBox1.SelectedItem).item.Id, this.listBox1.Text);
				if (frmPEnt.ShowDialog().Equals(System.Windows.Forms.DialogResult.OK))
					this.Close();
			}
		} 

	}
}
